package com.lianlb.controller;

import com.alibaba.fastjson.JSONObject;
import com.lianlb.entity.ZErrorMsg;
import com.lianlb.service.UserService;
import com.mysql.cj.xdevapi.JsonArray;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 首页控制器
 */
@Controller
@RequestMapping("/index")
public class IndexController {

    private static Log logger = LogFactory.getLog(IndexController.class);

    @Autowired
    private UserService userService;

    /**
     * 欢迎页
     * @param request
     * @return
     */
    @RequestMapping(params = "hello")
    public String index(HttpServletRequest request) {

        ZErrorMsg user = userService.getUser();

        logger.debug(JSONObject.toJSONString(user));
        request.setAttribute("user", user);

        return "hello";
    }

    /**
     * 测试json数据
     * @return
     */
    @ResponseBody
    @RequestMapping(params = "json")
    public JSONObject json(HttpServletRequest request) {

        JSONObject json = new JSONObject();
        json.put("id", 111232);
        json.put("name", "lianlb");
        json.put("age", 23);
        logger.info(json);

        request.setAttribute("json", json);
        return json;
    }
}
